#!/usr/bin/env python
# -*- coding: utf-8 -*- 

'''
authors: Fábio Martins nº6041 and Miguel Travassos nº6312
'''

import matplotlib.pyplot as grafico
from statistics import*
import numpy as np
import thread
from threading import Thread

class makeGraph(object):
	"""Classe que vai gerar os gráficos"""
	def __init__(self):
		super(makeGraph, self).__init__()
		self.grafico = grafico
		self.statistcs = statisticsClass()

	def grapicsInstitution(self,statistics):
		""" funçao que vai criar os graficos das instituiçoes"""

		self.statistics = statistics
		labelsX = ["Colocados","Percentagem","Vagas"]


		fig = self.grafico.figure(figsize=(7,5), dpi=70)
		ind = np.arange(len(labelsX))
		
		width=0.10
		ax = fig.add_subplot(111)

		ax.set_xlabel(self.statistics[0][0])

		self.colocados = [self.statistics[0][1]]
		self.percentagem = [self.statistics[0][2]]
		self.vagas = [self.statistics[0][3]]
		
		
		rectColocados = ax.bar(ind[0], self.colocados, width,color='r')
		rectPercentagem = ax.bar(ind[1], self.percentagem, width,color='yellow')
		rectVagas = ax.bar(ind[2], self.vagas, width,color='b')
		
		ax.set_title("Instituto")
		ax.set_xticks(ind + width/2)
		ax.set_xticklabels(labelsX)
		ax.legend( (rectColocados[0], rectPercentagem[0],rectVagas[0]), (labelsX[0], labelsX[1],labelsX[2]) )
		
		rects =[rectColocados,rectPercentagem,rectVagas]
		
		return fig


		

	def graphicsDistrict(self,statistics):
		"""Funçao que vai criar os gráficos dos distritos"""

		self.statistics = statistics
		
		fig = self.grafico.figure(figsize=(7,4), dpi=80)
		
		width=0.50
		
		ax = fig.add_subplot(1,1,1)

		ax.set_xlabel(self.statistics[0][0])

		self.y = [self.statistics[i][1] for i in xrange(1,len(self.statistics))]
		
		labelsX =[self.statistics[i][0].decode("utf-8") for i in xrange(1,len(self.statistics))]

		ind = np.arange(len(labelsX))
		
		colors = ['r','b','g','yellow']
		rect1 = ax.bar(ind, self.y, width,color=colors)

		title = self.statistics[0][1]

		ax.set_title(title)
		ax.set_xticks(ind+ width/2)
		ax.set_xticklabels(labelsX)

		

		fig.autofmt_xdate()

		for rect in rect1:
			height = rect.get_height()
			if not height == 0:
				ax.text(rect.get_x()+rect.get_width()/2., height, '%d'%int(height),
					ha='center', va='bottom')
		

		return fig
	
	def labelBar(self,rects):
		for rect in rects:
			height = rect.get_height()
			if not height == 0:
				ax.text(rect.get_x()+rect.get_width()/2., height, '%d'%int(height),
					ha='center', va='bottom')
